using UnityEditor;

namespace UnityXEditor
{
    public static class SVNTool
    {
        [MenuItem("Assets/SVN 更新", false, 20)]
        private static void Update()
        {
            string[] strs = Selection.assetGUIDs;
            if (strs.Length > 0)
            {
                string fpath = AssetDatabase.GUIDToAssetPath(strs[0]);
                string path = "\"" + fpath;
                if (fpath != "Assets")
                    path += "*" + fpath + ".meta";
                for (int i = 1; i < strs.Length; i++)
                {
                    fpath = AssetDatabase.GUIDToAssetPath(strs[i]);
                    path += "*" + fpath;
                    if (fpath != "Assets")
                        path += "*" + fpath + ".meta";
                }
                path += "\"";
                Update(path);
            }
        }
        /// <summary>
        /// SVN更新
        /// </summary>
        /// <param name="path"></param>
        public static void Update(string path)
        {
            System.Diagnostics.Process process = new System.Diagnostics.Process();
            process.StartInfo.FileName = "TortoiseProc.exe";
            process.StartInfo.Arguments = "/command:update /path:" + path;
            process.Start();
        }

        [MenuItem("Assets/SVN 提交", false, 21)]
        private static void Commit()
        {
            string[] strs = Selection.assetGUIDs;
            if (strs.Length > 0)
            {
                string fpath = AssetDatabase.GUIDToAssetPath(strs[0]);
                string path = "\"" + fpath;
                if (fpath != "Assets")
                    path += "*" + fpath + ".meta";
                for (int i = 1; i < strs.Length; i++)
                {
                    fpath = AssetDatabase.GUIDToAssetPath(strs[i]);
                    path += "*" + fpath;
                    if (fpath != "Assets")
                        path += "*" + fpath + ".meta";
                }
                path += "\"";
                Commit(path);
            }
        }
        /// <summary>
        /// SVN提交
        /// </summary>
        /// <param name="path"></param>
        public static void Commit(string path)
        {
            System.Diagnostics.Process process = new System.Diagnostics.Process();
            process.StartInfo.FileName = "TortoiseProc.exe";
            process.StartInfo.Arguments = "/command:commit /path:" + path;
            process.Start();
        }

        [MenuItem("Assets/SVN 更新", true)]
        [MenuItem("Assets/SVN 提交", true)]
        private static bool AssetsSelectionTest()
        {
            return Selection.assetGUIDs.Length > 0;
        }
    }

}